package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void AddShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前用户ID
        Long userID= BaseContext.getCurrentId();
        //设置用户ID
        shoppingCart.setUserId(userID);

        //动态查询菜品/套餐的信息
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断当前加入到购物车中的商品是否已经存在了
        if(list!=null && list.size()!=0){
            //如果已经存在，只需要将数量加一

            //购物车只会查出一条唯一数据
            ShoppingCart cart = list.get(0);
            //更新数量塑胶
            cart.setNumber(cart.getNumber()+1);
            //修改数据
            shoppingCartMapper.UpdateCartByID(cart);
        }else {
            //如果不存在，需要插入一条购物车数据

            //判断插入的是菜品还是套餐
            Long dishID=shoppingCart.getDishId();
            if(dishID!=null){
                //菜品
                //通过菜品ID 查询菜品数据
                Dish dish = dishMapper.getById(dishID);
                //将数据设置进去  菜品名称、图片、价格
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //套餐
                Long setmealID=shoppingCart.getSetmealId();
                //通过套餐ID查询套餐数据
                Setmeal setmeal = setmealMapper.selectByID(setmealID);
                //设置 套餐名称、图片、价格
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //设置套餐数量、创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取用户ID
        Long userId = BaseContext.getCurrentId();
        //设置用户id
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(userId);
        //查询
        List<ShoppingCart> shoppingCarts=shoppingCartMapper.list(shoppingCart);
        return shoppingCarts;
    }

    /**
     * 清空购物车
     */
    @Override
    public void DeleShoppingCart() {
        //获取用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.DeleShoppingCart(userId);
    }
}
